﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Globalization

Namespace Infrastructure

    Public Class QueueRequestValidator
        Implements IStorageRequestValidator
        Private Const BaseUrl As String = "/AzureQueuesProxy.axd"

        Private ReadOnly userPrivilegesRepository As IUserPrivilegesRepository

        Public Sub New()
            Me.New(New UserTablesServiceContext())
        End Sub

        <CLSCompliant(False)>
        Public Sub New(ByVal userPrivilegesRepository As IUserPrivilegesRepository)
            Me.userPrivilegesRepository = userPrivilegesRepository
        End Sub

        Public Function DoesRequestApply(ByVal resourceUri As Uri) As Boolean Implements IStorageRequestValidator.DoesRequestApply
            Return resourceUri.AbsolutePath.StartsWith(BaseUrl, StringComparison.OrdinalIgnoreCase)
        End Function

        Public Function ValidateRequest(ByVal userId As String, ByVal request As HttpRequest) As Boolean Implements IStorageRequestValidator.ValidateRequest
            If (Not Me.CanUseQueues(userId)) Then
                Return False
            End If

            Dim queueName = StorageRequestAnalyzer.GetRequestedQueue(request)
            If (Not Me.CanUseQueue(userId, queueName, request)) Then
                Return False
            End If

            Return True
        End Function

        Private Function CanUseQueue(ByVal userId As String, ByVal queueName As String, ByVal request As HttpRequest) As Boolean
            If String.IsNullOrWhiteSpace(queueName) Then
                Return True
            End If

            Dim publicQueuePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", queueName, PrivilegeConstants.PublicQueuePrivilegeSuffix)
            If (Not Me.userPrivilegesRepository.PublicPrivilegeExists(publicQueuePrivilege)) Then
                Dim accessQueuePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", queueName, PrivilegeConstants.QueuePrivilegeSuffix)
                If (Not Me.userPrivilegesRepository.HasUserPrivilege(userId, accessQueuePrivilege)) Then
                    ' Check if the user is creating a new queue.
                    Return StorageRequestAnalyzer.IsCreatingQueue(request)
                End If
            End If

            Return True
        End Function

        Private Function CanUseQueues(ByVal userId As String) As Boolean
            Return Me.userPrivilegesRepository.HasUserPrivilege(userId, PrivilegeConstants.QueuesUsagePrivilege)
        End Function
    End Class
End Namespace